package com.example.hssmgs.admin;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hssmgs.R;
import com.example.hssmgs.Student.CommonDatabase;
import com.example.hssmgs.Student.new_info;

public class Admin_oper extends Activity {
    EditText add_user_status;
    EditText add_user;
    EditText add_user_pw;
    EditText delete_user_id;
    Button add_user_bt;
    Button back1;
    Button delete_bt;
    Button back2;
    SQLiteDatabase db;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_oper);

        initView();

//        final String status = add_user_status.getText().toString();

        add_user_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String status = add_user_status.getText().toString();
                if (status.equals("")){
                    Toast toast = Toast.makeText(getApplicationContext(),"未填写职业类型",Toast.LENGTH_SHORT);
                    toast.show();
                }else if (status.equals("student")){
                    ContentValues values = new ContentValues();
                    values.put("account",add_user.getText().toString());
                    values.put("password",add_user_pw.getText().toString());
                    db.insert("student_pw",null,values);

                    ContentValues values2 = new ContentValues();
                    values2.put("id",add_user.getText().toString());
                    values2.put("sex","男");
                    db.insert("student_db",null,values2);

                    Toast toast=Toast.makeText(getApplicationContext(),"添加成功",Toast.LENGTH_SHORT);
                    toast.show();
                    finish();
                }else if (status.equals("teacher")){
                    ContentValues values = new ContentValues();
                    values.put("account",add_user.getText().toString());
                    values.put("password",add_user_pw.getText().toString());
                    db.insert("teacher_pw",null,values);

                    ContentValues values2 = new ContentValues();
                    values2.put("id",add_user.getText().toString());
                    values2.put("name","未知");
                    db.insert("teacher_db",null,values2);

                    Toast toast=Toast.makeText(getApplicationContext(),"添加成功",Toast.LENGTH_SHORT);
                    toast.show();
                    finish();
                }
            }
        });

        back1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        delete_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String status = add_user_status.getText().toString();
                if (status.equals("")){
                    Toast toast = Toast.makeText(getApplicationContext(),"未填写职业类型",Toast.LENGTH_SHORT);
                    toast.show();
                }else if (status.equals("student")){
                    ContentValues values = new ContentValues();
                    db.delete("student_db","id=?",new String[]{delete_user_id.getText().toString()});
                    db.delete("student_pw","account=?",new String[]{delete_user_id.getText().toString()});

                    Toast toast=Toast.makeText(getApplicationContext(),"删除成功",Toast.LENGTH_SHORT);
                    toast.show();
                    finish();
                }else if (status.equals("teacher")){
                    ContentValues values = new ContentValues();
                    db.delete("teacher_db","id=?",new String[]{delete_user_id.getText().toString()});
                    db.delete("teacher_pw","account=?",new String[]{delete_user_id.getText().toString()});

                    Toast toast=Toast.makeText(getApplicationContext(),"删除成功",Toast.LENGTH_SHORT);
                    toast.show();
                    finish();
                }
            }
        });
//
        back2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initView() {
        //初始化数据库对象
        CommonDatabase commonDatabase = new CommonDatabase();
        db = commonDatabase.getSqliteObject(Admin_oper.this,"test2.db");
        add_user_status=findViewById(R.id.add_user_status);
        add_user=findViewById(R.id.add_user_id);
        add_user_pw=findViewById(R.id.add_user_pw);
        delete_user_id=findViewById(R.id.delete_user_id);
        add_user_bt=findViewById(R.id.add_user_bt);
        back1=findViewById(R.id.back1);
        delete_bt=findViewById(R.id.delete_bt);
        back2=findViewById(R.id.back2);
    }
}
